package com.jzo2o.customer.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.api.publics.SmsCodeApi;
import com.jzo2o.common.constants.UserType;
import com.jzo2o.common.enums.SmsBussinessTypeEnum;
import com.jzo2o.common.expcetions.BadRequestException;
import com.jzo2o.customer.mapper.ServeProviderMapper;
import com.jzo2o.customer.model.domain.ServeProvider;
import com.jzo2o.customer.model.dto.request.RegisterAccountReqDTO;
import com.jzo2o.customer.service.IServeProviderService;
import com.jzo2o.customer.service.RegisterService;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class RegisterServiceImpl extends ServiceImpl<ServeProviderMapper, ServeProvider> implements RegisterService {
    // 验证码
    @Resource
    private SmsCodeApi smsCodeApi;
    // 自动注册
    @Resource
    private IServeProviderService serveProviderService;
    // 密码加密
    @Resource
    private PasswordEncoder passwordEncoder;
    /**
     * 机构注册接口
     */
    @Override
    public void Account(RegisterAccountReqDTO registerAccountReqDTO) {
        // 1、校验手机号是否存在
        ServeProvider existServeProvider = lambdaQuery().eq(ServeProvider::getPhone, registerAccountReqDTO.getPhone()).one();
        if (existServeProvider != null) {
            if(existServeProvider.getType().equals(UserType.INSTITUTION)){
                throw new BadRequestException("该账号已被机构注册");
            }
        }
        // 2、远程调用publics服务校验验证码是否正确
        boolean verifyResult = smsCodeApi.verify(registerAccountReqDTO.getPhone(), SmsBussinessTypeEnum.INSTITION_REGISTER, registerAccountReqDTO.getVerifyCode()).getIsSuccess();
        if(!verifyResult) {
            throw new BadRequestException("验证码错误，请重新获取");
        }
        // 3、自动注册
        if(existServeProvider == null) {
            String encode = passwordEncoder.encode(registerAccountReqDTO.getPassword());
            serveProviderService.add(registerAccountReqDTO.getPhone(), UserType.INSTITUTION, encode);
        }
    }

    /**
     * 忘记密码接口
     */
    @Override
    public void ResetPassword(RegisterAccountReqDTO registerAccountReqDTO) {
        // 1、校验手机号是否存在
        ServeProvider serveProvider = lambdaQuery().eq(ServeProvider::getPhone, registerAccountReqDTO.getPhone()).one();
        if (serveProvider == null) {
            if(serveProvider.getType().equals(UserType.INSTITUTION)){
                throw new BadRequestException("该账号没被注册");
            }
        }
        // 2、远程调用publics服务校验验证码是否正确
        boolean verifyResult = smsCodeApi.verify(registerAccountReqDTO.getPhone(), SmsBussinessTypeEnum.INSTITUTION_RESET_PASSWORD, registerAccountReqDTO.getVerifyCode()).getIsSuccess();
        if(!verifyResult) {
            throw new BadRequestException("验证码错误，请重新获取");
        }
        // 3、密码加密
        String encode = passwordEncoder.encode(registerAccountReqDTO.getPassword());
        serveProvider.setPassword(encode);
        // 4、更新密码
        baseMapper.updateById(serveProvider);
    }
}
